package org.nanotek.sign;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Files;

import org.apache.log4j.Logger;
import org.nanotek.Signer;

public abstract class FileSignerSupport<T extends File,S> implements Signer<T,S>{

	private Logger log = Logger.getLogger(FileSignerSupport.class);
	
	public FileSignerSupport() {
	}

	@Override
	public S sign(T toSign) {
		return signFile(toSign);
	}
	
	protected abstract S signFile(T toSign);

	public ByteBuffer readFile(File file) {
		ByteBuffer buffer = null;
		try {
			byte [] fileByteArray = Files.readAllBytes(file.toPath());
			buffer = ByteBuffer.allocate(fileByteArray.length);
			buffer.put(fileByteArray);
			buffer.flip();
		} catch (Exception e) {
			e.printStackTrace();
			log.debug(e);
		}
		return  buffer; 
	}
}
